package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.api.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "商品分类后台管理")
public class CategoryController extends BaseController {
    @Resource
    private CategoryService categoryService;

    @Operation(summary = "获取商品分类列表")
    @GetMapping("/{parentId}")
    @RequiresPermissions("product:category:list")
    public AjaxResult listInfo(@PathVariable Long parentId) {
        List<Category> categoryList = categoryService.listInfo(parentId);
        return success(categoryList);
    }

    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("product:category:export")
    @Operation(summary = "导出商品分类列表")
    @PostMapping("/export")
    public void exportCategory(HttpServletResponse response) {
        categoryService.exportCategory(response);
    }

    @Log(title = "分类管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("product:category:import")
    @Operation(summary = "导入商品分类列表")
    @PostMapping("/import")
    public AjaxResult importCategory(MultipartFile file) {
        return toAjax(categoryService.importCategory(file));
    }

    @InnerAuth
    @Operation(summary = "内部调用方法：获取商品分类树")
    @GetMapping("/listCategoryTree")
    public R<List<Category>> listCategoryTree() {
        List<Category> categoryList = categoryService.listCategoryTree();
        return R.ok(categoryList);
    }
}
